package com.ruoyi.ai.service.impl;

import com.ruoyi.ai.utils.AiUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.ai.domain.PyMessage;
import com.ruoyi.ai.mapper.PyMessageMapper;
import com.ruoyi.ai.service.IPyMessageService;
import com.ruoyi.common.utils.IDUtils;
import com.ruoyi.common.utils.SecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 消息信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-01
 */
@Service
public class PyMessageServiceImpl implements IPyMessageService {
    @Autowired
    private PyMessageMapper pyMessageMapper;

    @Resource
    private AiUtil aiUtil;

    /**
     * 查询消息信息
     *
     * @param id 消息信息主键
     * @return 消息信息
     */
    @Override
    public PyMessage selectPyMessageById(String id) {
        return pyMessageMapper.selectPyMessageById(id);
    }

    /**
     * 查询消息信息列表
     *
     * @param pyMessage 消息信息
     * @return 消息信息
     */
    @Override
    public List<PyMessage> selectPyMessageList(PyMessage pyMessage) {
        return pyMessageMapper.selectPyMessageList(pyMessage);
    }

    /**
     * 新增消息信息
     *
     * @param pyMessage 消息信息
     * @return 结果
     */
    @Override
    public int insertPyMessage(PyMessage pyMessage) {
        pyMessage.setCreateTime(DateUtils.getNowDate());
        return pyMessageMapper.insertPyMessage(pyMessage);
    }

    /**
     * 修改消息信息
     *
     * @param pyMessage 消息信息
     * @return 结果
     */
    @Override
    public int updatePyMessage(PyMessage pyMessage) {
        pyMessage.setUpdateTime(DateUtils.getNowDate());
        return pyMessageMapper.updatePyMessage(pyMessage);
    }

    /**
     * 批量删除消息信息
     *
     * @param ids 需要删除的消息信息主键
     * @return 结果
     */
    @Override
    public int deletePyMessageByIds(String[] ids) {
        return pyMessageMapper.deletePyMessageByIds(ids);
    }

    /**
     * 删除消息信息信息
     *
     * @param id 消息信息主键
     * @return 结果
     */
    @Override
    public int deletePyMessageById(String id) {
        return pyMessageMapper.deletePyMessageById(id);
    }

    @Override
    @Transactional
    public AjaxResult sendMessage(String question) {
        IDUtils idUtils = new IDUtils();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        PyMessage userMessage = new PyMessage();
        userMessage.setId(idUtils.nextId()+"");
        userMessage.setText(question);
        userMessage.setOwner(loginUser.getUserId().toString());
        userMessage.setBotFlag("0");
        userMessage.setCreateTime(new Date());
        pyMessageMapper.insertPyMessage(userMessage);



        String answer = aiUtil.sendMessage(question);
        PyMessage pyMessage = new PyMessage();
        pyMessage.setId(idUtils.nextId()+"");
        pyMessage.setText(answer);
        pyMessage.setOwner(loginUser.getUserId().toString());
        pyMessage.setBotFlag("2");
        pyMessage.setCreateTime(new Date());
        pyMessageMapper.insertPyMessage(pyMessage);

        return AjaxResult.success("ok",answer);
    }
}
